package com.szh.cloud.spring.control;

import com.alibaba.fastjson.JSON;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.servlet.LocaleResolver;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class BaseController
{
    @Resource
    private MessageSource messageSource;
    public String successJSON(String key,Object ... args){
        Locale locale = LocaleContextHolder.getLocale();
        String msg = messageSource.getMessage(key,args,locale);
        Map<String,Object> mapRst = new HashMap<>();
        mapRst.put("success",true);
        mapRst.put("msg",msg);
        mapRst.put("description",msg);

        return JSON.toJSONString(mapRst);

    }

    public String successCodeJSON(String key,Object code,Object ... args){
        Locale locale = LocaleContextHolder.getLocale();
        String msg = messageSource.getMessage(key,args,locale);
        Map<String,Object> mapRst = new HashMap<>();
        mapRst.put("success",true);
        mapRst.put("msg",msg);
        mapRst.put("code",code);
        mapRst.put("description",msg);

        return JSON.toJSONString(mapRst);

    }

    public String failJSON(String key,Object ... args){
        Locale locale = LocaleContextHolder.getLocale();
        String msg = messageSource.getMessage(key,args,locale);
        Map<String,Object> mapRst = new HashMap<>();
        mapRst.put("success",false);
        mapRst.put("msg",msg);
        mapRst.put("description",msg);

        return JSON.toJSONString(mapRst);

    }
}
